package com.lvhx.sofa3.client.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.lvhx.sofa3.client.dao.entity.User;
import com.lvhx.sofa3.client.dao.mapper.extend.UserExtendMapper;
import com.lvhx.sofa3.client.service.TestService;
import com.lvhx.sofa3.server.facade.Sofa3HelloService;
import com.lvhx.sofa3.server.facade.Test1Reponse;
import com.lvhx.sofa3.server.facade.Test1Request;
import com.lvhx.sofa3.server.facade.Test2Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {

    @Resource
    private Sofa3HelloService sofa3HelloService;

    @Resource
    private UserExtendMapper userExtendMapper;
    @Resource
    private TestService testService;

    // 注解方式引用sofaRPC服务
    @SofaReference(interfaceType =Test2Service.class, binding =@SofaReferenceBinding(bindingType ="bolt",directUrl = "127.0.0.1:12200"))
    private Test2Service test2Service;

    @RequestMapping("/test1")
//    @Transactional
    public String test1() throws Exception{
        Test1Request request = new Test1Request();
        request.setRequestName("hello word!");

//        Test1Reponse reponse = sofa3HelloService.test1(request);

        User user = userExtendMapper.selectByPrimaryKey(1L);

        testService.changeUserName(1L,"zhangsan");

        int i = 1/0;

        return JSON.toJSONString(user);
    }

    @GetMapping("/test2")
    public String test2() throws Exception{
        Test1Request request = new Test1Request();
        request.setRequestName("hello word!");
        Test1Reponse reponse = test2Service.fun_1(request);
        return JSON.toJSONString(reponse);
    }



}
